﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConAppCore.Test
{
    internal class CodeConvert
    {
        public static void Test()
        {
            ChangeAggressiveInlining();
        }

        private static void ChangeAggressiveInlining()
        {
            /*

#if !NET4_0
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif

 */

            string srcDir = @"D:\Projects\24_013\ZD784.FZ013\ClassLibrary140";
            string[] fileNames = Directory.GetFiles(srcDir, "*.cs", SearchOption.AllDirectories);
            StringBuilder sb = new StringBuilder();
            bool flag;
            foreach (string fileName in fileNames)
            {
                sb.Clear();
                flag = false;
                string[] lines = File.ReadAllLines(fileName);

                for (int i = 0; i < lines.Length; i++)
                {
                    if ((lines[i].Contains(@"[MethodImpl(MethodImplOptions.AggressiveInlining)]")|| lines[i].Contains(@"[MethodImplAttribute(MethodImplOptions.AggressiveInlining)]")) &&
                        !lines[i - 1].Contains(@"#if"))
                    {
                        sb.AppendLine(@"#if !NET4_0");
                        sb.AppendLine(lines[i]);
                        sb.AppendLine(@"#endif");
                        flag = true;
                    }
                    else
                    {
                        sb.AppendLine(lines[i]);
                    }
                }

                if (flag)
                {
                    File.WriteAllText(fileName, sb.ToString());
                }
            }
        }
    }
}
